home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1997 December / PC Pro December 1997 CD-Rom coverdisc.iso / symantec / dbAnywh / JAVA.BIN / CLASSES.ZIP / sun / applet / AppletSecurity.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-12-14  |  7.2 KB  |  331 lines

  1. package sun.applet;
  2.  
  3. import java.io.File;
  4. import java.io.FileDescriptor;
  5. import java.net.InetAddress;
  6. import java.net.URL;
  7. import java.net.UnknownHostException;
  8. import java.util.StringTokenizer;
  9. import java.util.Vector;
  10.  
  11. public class AppletSecurity extends SecurityManager {
  12.    private static boolean debug;
  13.    boolean initACL;
  14.    String[] readACL;
  15.    String[] writeACL;
  16.    int networkMode;
  17.    static final int NETWORK_NONE = 1;
  18.    static final int NETWORK_HOST = 2;
  19.    static final int NETWORK_UNRESTRICTED = 3;
  20.  
  21.    public AppletSecurity() {
  22.       this.reset();
  23.    }
  24.  
  25.    public void reset() {
  26.       String var1 = System.getProperty("appletviewer.security.mode");
  27.       if (var1 == null) {
  28.          var1 = "host";
  29.       }
  30.  
  31.       if (var1.equals("unrestricted")) {
  32.          this.networkMode = 3;
  33.       } else if (var1.equals("none")) {
  34.          this.networkMode = 1;
  35.       } else {
  36.          this.networkMode = 2;
  37.       }
  38.    }
  39.  
  40.    boolean fromApplet() {
  41.       return ((SecurityManager)this).classLoaderDepth() == 1;
  42.    }
  43.  
  44.    boolean inApplet() {
  45.       return ((SecurityManager)this).inClassLoader();
  46.    }
  47.  
  48.    public Object getSecurityContext() {
  49.       AppletClassLoader var1 = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
  50.       return var1 == null ? null : var1.base;
  51.    }
  52.  
  53.    public synchronized void checkCreateClassLoader() {
  54.       if (((SecurityManager)this).classLoaderDepth() == 2) {
  55.          throw new AppletSecurityException("classloader");
  56.       }
  57.    }
  58.  
  59.    public synchronized void checkAccess(Thread var1) {
  60.       if (((SecurityManager)this).classLoaderDepth() == 2 && !(var1.getThreadGroup() instanceof AppletThreadGroup)) {
  61.          throw new AppletSecurityException("thread");
  62.       }
  63.    }
  64.  
  65.    public synchronized void checkAccess(ThreadGroup var1) {
  66.       if (((SecurityManager)this).classLoaderDepth() == 4 && !(var1 instanceof AppletThreadGroup)) {
  67.          throw new AppletSecurityException("threadgroup", var1.toString());
  68.       }
  69.    }
  70.  
  71.    public synchronized void checkExit(int var1) {
  72.       if (this.inApplet()) {
  73.          throw new AppletSecurityException("exit", String.valueOf(var1));
  74.       }
  75.    }
  76.  
  77.    public synchronized void checkExec(String var1) {
  78.       if (this.inApplet()) {
  79.          throw new AppletSecurityException("exec", var1);
  80.       }
  81.    }
  82.  
  83.    public synchronized void checkLink(String var1) {
  84.       switch (((SecurityManager)this).classLoaderDepth()) {
  85.          case 2:
  86.          case 3:
  87.             throw new AppletSecurityException("link", var1);
  88.          default:
  89.       }
  90.    }
  91.  
  92.    public synchronized void checkPropertiesAccess() {
  93.       if (((SecurityManager)this).classLoaderDepth() == 2) {
  94.          throw new AppletSecurityException("properties");
  95.       }
  96.    }
  97.  
  98.    public synchronized void checkPropertyAccess(String var1) {
  99.       if (((SecurityManager)this).classLoaderDepth() == 2 && !"true".equalsIgnoreCase(System.getProperty(var1 + ".applet"))) {
  100.          throw new AppletSecurityException("properties");
  101.       }
  102.    }
  103.  
  104.    void parseACL(Vector var1, String var2, String var3) {
  105.       StringTokenizer var4 = new StringTokenizer(var2, System.getProperty("path.separator"));
  106.  
  107.       while(var4.hasMoreTokens()) {
  108.          String var5 = var4.nextToken();
  109.          if (var5.startsWith("~")) {
  110.             var1.addElement(System.getProperty("user.home") + var5.substring(1));
  111.          } else if (var5.equals("+")) {
  112.             if (var3 != null) {
  113.                this.parseACL(var1, var3, (String)null);
  114.             }
  115.          } else {
  116.             var1.addElement(var5);
  117.          }
  118.       }
  119.  
  120.    }
  121.  
  122.    String[] parseACL(String var1, String var2) {
  123.       if (var1 == null) {
  124.          return new String[0];
  125.       } else if (var1.equals("*")) {
  126.          return null;
  127.       } else {
  128.          Vector var3 = new Vector();
  129.          this.parseACL(var3, var1, var2);
  130.          String[] var4 = new String[var3.size()];
  131.          var3.copyInto(var4);
  132.          return var4;
  133.       }
  134.    }
  135.  
  136.    void initializeACLs() {
  137.       this.readACL = this.parseACL(System.getProperty("acl.read"), System.getProperty("acl.read.default"));
  138.       this.writeACL = this.parseACL(System.getProperty("acl.write"), System.getProperty("acl.write.default"));
  139.       this.initACL = true;
  140.    }
  141.  
  142.    public synchronized void checkRead(String var1) {
  143.       AppletClassLoader var2 = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
  144.       if (var2 != null) {
  145.          this.checkRead(var1, var2.base);
  146.       }
  147.  
  148.    }
  149.  
  150.    public synchronized void checkRead(String var1, URL var2) {
  151.       if (var2 != null) {
  152.          if (!this.initACL) {
  153.             this.initializeACLs();
  154.          }
  155.  
  156.          if (this.readACL != null) {
  157.             int var3 = this.readACL.length;
  158.  
  159.             while(var3-- > 0) {
  160.                if (var1.startsWith(this.readACL[var3])) {
  161.                   return;
  162.                }
  163.             }
  164.  
  165.             if (var2.getProtocol().equals("file")) {
  166.                String var4 = var2.getFile().replace('/', File.separatorChar);
  167.                if (var1.startsWith(var4)) {
  168.                   return;
  169.                }
  170.             }
  171.  
  172.             throw new AppletSecurityException("file.read", var1);
  173.          }
  174.       }
  175.    }
  176.  
  177.    public void checkRead(String var1, Object var2) {
  178.       this.checkRead(var1);
  179.       if (var2 != null) {
  180.          this.checkRead(var1, (URL)var2);
  181.       }
  182.  
  183.    }
  184.  
  185.    public synchronized void checkWrite(String var1) {
  186.       if (this.inApplet()) {
  187.          if (!this.initACL) {
  188.             this.initializeACLs();
  189.          }
  190.  
  191.          if (this.writeACL != null) {
  192.             int var2 = this.writeACL.length;
  193.  
  194.             while(var2-- > 0) {
  195.                if (var1.startsWith(this.writeACL[var2])) {
  196.                   return;
  197.                }
  198.             }
  199.  
  200.             throw new AppletSecurityException("file.write", var1);
  201.          }
  202.       }
  203.    }
  204.  
  205.    public synchronized void checkRead(FileDescriptor var1) {
  206.       if (this.inApplet() && !((SecurityManager)this).inClass("java.net.SocketInputStream") || !var1.valid()) {
  207.          throw new AppletSecurityException("fd.read");
  208.       }
  209.    }
  210.  
  211.    public synchronized void checkWrite(FileDescriptor var1) {
  212.       if (this.inApplet() && !((SecurityManager)this).inClass("java.net.SocketOutputStream") || !var1.valid()) {
  213.          throw new AppletSecurityException("fd.write");
  214.       }
  215.    }
  216.  
  217.    public synchronized void checkListen(int var1) {
  218.       if (this.inApplet()) {
  219.          throw new AppletSecurityException("socket.listen", String.valueOf(var1));
  220.       }
  221.    }
  222.  
  223.    public synchronized void checkAccept(String var1, int var2) {
  224.       if (this.inApplet()) {
  225.          throw new AppletSecurityException("socket.accept", var1 + ":" + var2);
  226.       }
  227.    }
  228.  
  229.    public synchronized void checkConnect(String var1, int var2) {
  230.       AppletClassLoader var3 = (AppletClassLoader)((SecurityManager)this).currentClassLoader();
  231.       if (var3 != null) {
  232.          int var4 = ((SecurityManager)this).classDepth("sun.net.www.http.HttpClient");
  233.          if (var4 <= 1) {
  234.             this.checkConnect(var3.base.getHost(), var1);
  235.          }
  236.       }
  237.    }
  238.  
  239.    public void checkConnect(String var1, int var2, Object var3) {
  240.       this.checkConnect(var1, var2);
  241.       if (var3 != null) {
  242.          this.checkConnect(((URL)var3).getHost(), var1);
  243.       }
  244.  
  245.    }
  246.  
  247.    public synchronized void checkConnect(String var1, String var2, boolean var3) {
  248.       if (var1 != null) {
  249.          switch (this.networkMode) {
  250.             case 1:
  251.                throw new AppletSecurityException("socket.connect", var1 + "->" + var2);
  252.             case 2:
  253.                try {
  254.                   super.inCheck = true;
  255.                   if (var1.equals(var2)) {
  256.                      try {
  257.                         InetAddress var14 = InetAddress.getByName(var2);
  258.                         return;
  259.                      } catch (UnknownHostException var12) {
  260.                         if (!var3) {
  261.                            throw new AppletSecurityException("Could not resolve IP for host " + var2 + ". See the trustProxy property.");
  262.                         }
  263.  
  264.                         return;
  265.                      }
  266.                   }
  267.  
  268.                   try {
  269.                      InetAddress var6 = InetAddress.getByName(var2);
  270.                      InetAddress var7 = InetAddress.getByName(var1);
  271.                      if (!var7.equals(var6)) {
  272.                         throw new AppletSecurityException("Couldn't connect to " + var2 + " with origin from " + var1);
  273.                      }
  274.                   } catch (UnknownHostException var11) {
  275.                      throw new AppletSecurityException("Couldn't resolve IP for host " + var2 + " or for " + var1 + ".");
  276.                   }
  277.                } finally {
  278.                   super.inCheck = false;
  279.                }
  280.  
  281.                return;
  282.             case 3:
  283.                return;
  284.             default:
  285.                throw new AppletSecurityException("connect", var1 + "->" + var2);
  286.          }
  287.       }
  288.    }
  289.  
  290.    public synchronized void checkConnect(String var1, String var2) {
  291.       this.checkConnect(var1, var2, Boolean.getBoolean("trustProxy"));
  292.    }
  293.  
  294.    public synchronized boolean checkTopLevelWindow(Object var1) {
  295.       return !((SecurityManager)this).inClassLoader();
  296.    }
  297.  
  298.    public synchronized void checkPackageAccess(String var1) {
  299.       int var2 = var1.indexOf(46);
  300.       if (var2 > 0) {
  301.          var1 = var1.substring(0, var2);
  302.       }
  303.  
  304.       if (((SecurityManager)this).inClassLoader() && Boolean.getBoolean("package.restrict.access." + var1)) {
  305.          throw new SecurityException();
  306.       }
  307.    }
  308.  
  309.    public synchronized void checkPackageDefinition(String var1) {
  310.       int var2 = var1.indexOf(46);
  311.       if (var2 > 0) {
  312.          var1 = var1.substring(0, var2);
  313.       }
  314.  
  315.       if (((SecurityManager)this).inClassLoader() && Boolean.getBoolean("package.restrict.definition." + var1)) {
  316.          throw new SecurityException();
  317.       }
  318.    }
  319.  
  320.    public synchronized void checkSetFactory() {
  321.       throw new SecurityException();
  322.    }
  323.  
  324.    public void debug(String var1) {
  325.       if (debug) {
  326.          System.err.println(var1);
  327.       }
  328.  
  329.    }
  330. }
  331.